package day13;

import java.awt.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.junit.Test;

import day11.Student;

public class JDBCTest {

	@Test
	public void test1() {
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Student> list = new ArrayList<Student>();
		String seachName = "张";
	
		try {
			connection = JDBCUtil.getConnection();
//			String sql = "select * from student where name like '张%'";
			String sql = "select * from student where name like ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, seachName + "%");
			//resultSet 是对数据库查询结果的封装
			resultSet = preparedStatement.executeQuery();
			//判断下一行有没有，有的话返回true并指向该行,没有的返回false
			while(resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id,name,age,gender);
				list.add(student);
			}
			
			for(Student student : list) {
				System.out.println(student);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.close(resultSet, connection, preparedStatement);
		}
	
		
	}
}
